package cate.game.role.pay.trademall.masterprivilege;

import cate.common.table.d.GDOperation;
import cate.common.table.d.GDPrivilege;
import cate.common.table.farm.adventure.row.AdventureCharpterRow;
import cate.common.table.trademall.privilege.PrivilegeMallItemRow;
import cate.common.util.GameResult;
import cate.common.util.TimeTool;
import cate.game.mail.po.MailBuilder;
import cate.game.res.MixRes;
import cate.game.res.MixResItem;
import cate.game.role.Role;
import cate.game.role.RolePart;
import com.fasterxml.jackson.annotation.JsonIgnore;
import easy.java.dev.note.NoteClass;
import easy.java.dev.note.NoteField;
import easy.java.practice.errorcode.EcResult;
import org.springframework.data.annotation.Transient;

import java.util.Calendar;

@NoteClass(value = "坊主特权项")
public class RolePrivilegeItem extends RolePart {

    @NoteField("特权ID")
    public int tid;

    @NoteField("特权购买时间")
    public long buyTime;

    @NoteField("特权过期时间")
    public long expiredTime;

    @NoteField("特权续费提醒时间")
    public long remindTime;

    @NoteField(value = "上次领取时间")
    private long lastGetTime;

    @JsonIgnore
    public MixRes m1Consume;

    @JsonIgnore
    public MixRes firstBuyReward;

    @JsonIgnore
    public MixRes dailyReward;


    public RolePrivilegeItem() {
    }

    public RolePrivilegeItem(int tid) {
        this.tid = tid;
    }

    @Override
    public void initialize(Role role) {
        super.initialize(role);
        if (lastGetTime == 0) {
            lastGetTime = buyTime;
        }
    }

    public RolePrivilegeItem(Role role, int tid) {
        super(role);
        this.tid = tid;
    }

    @JsonIgnore
    @Transient
    public PrivilegeMallItemRow getRow() {
        return role.getGame().table.tradeMall.privilegeMallItem.get(tid);
    }

    @JsonIgnore
    @Transient
    public MixRes getFirstBuyReward() {
        return new MixRes(getRow().firstBuyReward);
    }

    /**
     * 判断特权是否过期
     */
    @JsonIgnore
    public boolean expired() {
        long expiredTime = this.expiredTime;
        if (expiredTime == 0L) {
            return false;
        }
        return System.currentTimeMillis() > expiredTime;
    }

    /**
     * 过期时间
     */
    @JsonIgnore
    public long resetExpiredTime(boolean change) {
        if (getRow().cycleMilliSecond <= 0L || (change && getRow().forerverTimes > 0)) {
            return 0L;
        }
        if (expiredTime == 0 || expiredTime < System.currentTimeMillis()) {
            Calendar calendar = Calendar.getInstance();
            calendar.set(Calendar.SECOND, 0);
            calendar.set(Calendar.MINUTE, 0);
            calendar.set(Calendar.HOUR_OF_DAY, 0);
            calendar.add(Calendar.DATE, 1);
            return calendar.getTimeInMillis() + getRow().cycleMilliSecond - 1000L;
        }
        return this.expiredTime + getRow().cycleMilliSecond - 1000L;
    }

    private void refreshTimes(boolean change) {
        this.buyTime = System.currentTimeMillis();
        this.expiredTime = resetExpiredTime(change);
        if (this.expiredTime > 0) {
            this.remindTime = this.expiredTime - getRow().renewDayMilliSecond;
        } else {
            this.remindTime = 0;
        }
    }

    private void initLastGetTime() {
        this.lastGetTime = System.currentTimeMillis();
    }

    public EcResult<MixResItem> consume() {
        if (m1Consume == null) {
            m1Consume = new MixRes(getRow().price);

        }
        return m1Consume.consume(role, null);
    }

    private void buildReward() {
        firstBuyReward = new MixRes(getRow().firstBuyReward);
        dailyReward = new MixRes(getRow().dailyReward);
    }

    public void initPrivilegeItem(boolean change) {
        refreshTimes(change);
        initLastGetTime();
        buildReward();
    }

    /**
     * 购买特权检测
     */
    public GameResult<Void> checkBuy() {
        GameResult<Void> r = new GameResult<>();
        long now = System.currentTimeMillis();
        if (now < remindTime || expiredTime <= 0) {
            return r.fail("不能重复购买");
        }
        return r.success();
    }

    public void sendPrivilegeReward() {
        long nowDay = TimeTool.getZeroTimeStamp(System.currentTimeMillis());
        long supplyNum = calNum(nowDay);
        if (supplyNum <= 0) {
            return;
        }
        if (dailyReward == null || dailyReward.items.size() == 0) {
            return;
        }
        if (getRow().mailId == 0) {
            return;
        }
        MixRes sendRes = new MixRes();
        sendRes.addList(dailyReward.copy());
        sendRes.multiply(supplyNum);
        sendMail(sendRes, getRow().mailId);
        lastGetTime = nowDay;
    }

    @Override
    public void onDaySpan(boolean silence) {
        sendPrivilegeReward();
    }

    private long calNum(long nowTime) {
        long lastDay = TimeTool.getZeroTimeStamp(lastGetTime);
        long settleTime = expiredTime > 0 ? Math.min(nowTime, expiredTime) : nowTime;
        return (settleTime - lastDay) / TimeTool.TimeCons.DAY;
    }

    private void sendMail(MixRes reward, int mailID) {
        sendMail(reward, mailID, GDOperation.A_PRIVILEGE_MALL_BUY);
    }

    public void sendMail(MixRes reward, int mailID, int operation) {
        MailBuilder mailBuilder = new MailBuilder();
        mailBuilder.setContentId(mailID).setReward(reward.copy())
                .setOperation(operation);
        role.getMail().add(mailBuilder.build());
    }
}
